package de.tud.etit.blipz.services.json;

import java.lang.reflect.Type;
import java.util.Date;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import de.tud.etit.blipz.entities.Position;

public class PositionDeserializer implements JsonDeserializer<Position> {

	@Override
	public Position deserialize(JsonElement jsonElement, Type type,
			JsonDeserializationContext ctx) throws JsonParseException {

		JsonObject jsonObject = jsonElement.getAsJsonObject();

		Long timestamp = jsonObject.get("timestamp").getAsLong();

		Date date = new Date(timestamp * 1000);
		Position position = new Position();
		position.setLatitudeE6(jsonObject.get("latitudeE6").getAsLong());
		position.setLongitutdeE6(jsonObject.get("longitudeE6").getAsLong());
		position.setTimestamp(date);

		return position;
	}

}
